package com.ido.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * Create by zoub
 * Date 2025/2/5 12:23 AM
 */
@Data
@Component
@ConfigurationProperties(prefix = "oss")
public class OssConfiguration {

    private String accessKeyId;

    private String accessKeySecret;

    private String endpoint;

    private String bucketName;

    private String domain;

    private String urlPrefix;


    /**
     * Oss初始化实例
     */
    @Bean
    public OSS ossClientInit() {
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }


    /**
     * 获取oss资源路径
     * @param path
     * @return
     */
    public String getOssResourceUrl(String path) {
        return StringUtils.isBlank(path) ? "" : urlPrefix + "/" + path;
    }

    public String replaceUrl(String path) {
        if (StringUtils.isBlank(path))return path;
        return path.replace(urlPrefix, "");
    }

}
